package linuxbox.poll.bean;

import java.util.*;

import linuxbox.exception.*;
import linuxbox.bean.*;
import linuxbox.util.*;

import linuxbox.poll.logic.BoxPollReplyEntry;

public class BoxPollReplyEntryBean extends BoxPollReplyEntry implements ITableBean
{
  protected DbAdapter m_db = null;
  protected boolean m_bTransaction = false;
  static protected String m_TableName = "box_poll_reply";

  public BoxPollReplyEntryBean(BaseIntKey pk, DbAdapter db)
  {
    this.init(pk, db);
  }

  public BoxPollReplyEntryBean(BaseIntKey pk)
  {
    this.init(pk, null);
  }

  public void init(BaseIntKey pk, DbAdapter db)
  {
    if (db != null)
    {
      m_db = db;
      m_bTransaction = true;
    }
    if (pk != null)
      setID(pk.getID());
  }

  public String getTableName()
  {
    return m_TableName;
  }

  public void setTableName(String str)
  {
    m_TableName = str;
  }

  public void makeConnection() throws Exception
  {
    if ( m_db == null )
      m_db = new DbAdapter(true);
  }

  public void closeConnection() throws Exception
  {
    if (m_db != null)
    {
      m_db.close();
      m_db = null;
    }
  }

  public void load() throws Exception
  {
    StringBuffer sql = new StringBuffer();

    sql.append("select id, pollid, choiceid, userid from ");
    sql.append(getTableName());
    sql.append(" where id = " + getID());

    makeConnection();
    m_db.executeQuery(sql.toString());
    if (m_db.next())
    {
      m_iID = m_db.getInt(1);           //id
      m_iPollID = m_db.getInt(2);       //pollid
      m_iChoiceID = m_db.getInt(3);     //choiceid
      m_UserID = m_db.getInt(4);    //userid
    }
    else
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.POLL, "LOAD", "Load error:  m_iID=" + m_iID);
    }
    if (!m_bTransaction)
      closeConnection();
  }

  public void store() throws Exception
  {
    StringBuffer sql = new StringBuffer();
    sql.append("UPDATE ");
    sql.append(getTableName());
    sql.append(" set ");

    DBUtil.setInteger(sql, "pollid", m_iPollID, true);
    DBUtil.setInteger(sql, "choiceid", m_iChoiceID, false);
    DBUtil.setInteger(sql, "userid", m_UserID, false);

    sql.append(" where id = " + m_iID);

    makeConnection();
    m_db.executeUpdate(sql.toString());
    if (!m_bTransaction)
      closeConnection();
  }

  public void refresh() throws Exception
  {
    load();
  }

  public void create() throws Exception
  {
    if (m_iID != 0)
    {
      String sql = "INSERT INTO ";
      sql += getTableName();
      sql += " (id) VALUES(" + m_iID + ")";
      makeConnection();
      m_db.executeUpdate(sql);
    }
    else
    {
      String sql = "select max(id) from ";
      sql += getTableName();
      makeConnection();
      m_db.executeQuery(sql);
      int max_id = 0;
      if (m_db.next())
      {
        max_id = m_db.getInt(1);
      }
      max_id++;
      m_iID = max_id;
      sql = "insert into ";
      sql += getTableName();
      sql += " (id) values(" + max_id + ")";
      m_db.executeUpdate(sql);
    }
    if (!m_bTransaction)
      closeConnection();
  }

  public void remove() throws Exception
  {
    String sql = "DELETE FROM ";
    sql += getTableName();
    sql += (" where id = " + m_iID);
    makeConnection();
    m_db.executeUpdate(sql);
    if (!m_bTransaction)
      closeConnection();
  }

  public int getCount(String sql) throws Exception
  {
    int count = 0;
    try
    {
      makeConnection();
      m_db.executeQuery(sql);
      if (m_db.next())
        count = m_db.getInt(1);
      if (!m_bTransaction)
        closeConnection();
    }
    catch(Exception e)
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.POLL, "findBySql", "Can't find record: " + e.getMessage());
    }
    return count;
  }

  public Object findByKey() throws Exception
  {
    BaseIntKey pk = null;
    try
    {
      String sql = "select id from ";
      sql += getTableName();
      sql += (" where id = " + m_iID);
      makeConnection();
      m_db.executeQuery(sql);
      if(m_db.next())
      {
        pk = new BaseIntKey();
        pk.setID(m_db.getInt(1));
      }
      if (!m_bTransaction)
        closeConnection();
    }
    catch(Exception e)
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.POLL, "findByKey", "Can't find record: " + e.getMessage());
    }
    return pk;
  }

  public Iterator findBySql(String sqlWhere)	throws Exception
  {
    Vector v = null;
    try
    {
      String sql = "select id from ";
      sql += getTableName();
      sql += sqlWhere;

      makeConnection();
      m_db.executeQuery(sql);
      BaseIntKey pk = null;
      v = new Vector();
      while (m_db.next())
      {
        pk = new BaseIntKey();
        pk.setID(m_db.getInt(1));
        v.addElement(pk);
      }
      if (!m_bTransaction)
        closeConnection();
    }
    catch(Exception e)
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.POLL, "findBySql", "Can't find record: " + e.getMessage());
    }
    return v.iterator();
  }

  public boolean updateBySql(String sql)	throws Exception
  {
    try
    {
      makeConnection();
      m_db.executeUpdate(sql);
      if (!m_bTransaction)
        closeConnection();
    }
    catch(Exception e)
    {
      if (!m_bTransaction)
        closeConnection();
      throw new LinuxboxException(ExceptionHelper.POLL, "updateBySql", "Can't update: " + e.getMessage());
    }
    return true;
  }
}
